const service = require("./index.service")
const {send,pageingQuery,isNextPageFn,getOffsetFn} = require("@/zy-common/utils")
const {
  FILTER_COMPOSITE,
  FILTER_DISCOUNT,
  FILTER_MIN_PRICE,
  FILTER_DISTRIBUTION,
  FILTER_ORDER_BY_OBJECT
} = require("./constant")

class FoodController {
  async getFoodClassify(ctx) {
    try {
      let res = await service.searchFoodClassify()
      ctx.body = res || []
    } catch (error) {
    }
  }
  async getAllFoodShop(ctx){
    try {
      const {pageIndex = 1,pageSize = 10} = ctx.request.body
      await pageingQuery(service.searchAllFoodShop,service.getAllFoodShopTotalSize,pageIndex,pageSize,ctx)
    } catch (error) {
      console.log(error);
    } 
  }

  async searchFoodShop(ctx){
    const {name} = ctx.request.body
    let res = await service.searchFoodShop(name)
    send(ctx,200,'200','查询商店成功',res)
  }

  async getShopById(ctx){
    const {shopId} = ctx.request.body
    let [res] = await service.getShopById(shopId)
    send(ctx,200,'200','查询商店详情成功',res)
  }

  async getDishesById(ctx){
    const {shopId} = ctx.request.body
    let res = await service.getDishesById(shopId)

    // 组合数据
    let newArr = []
    res.forEach(item => {
      let hasItem = newArr.find(i => i.classify_name === item.classify_name )
      if(!hasItem){
        newArr.push({
          classify_name:item.classify_name,
          children:[item],
          shop_id:shopId
        })
      }else{
        hasItem.children.push(item)
      }
    })
    send(ctx,200,'200','查询菜品数据成功',newArr)
  }

  async getDiscountDishes(ctx){
    let res = await service.getDiscountDishes()
    let obj = {
      "discount":res.slice(0,10),
      "limited":res.reverse().slice(0,10),
      "seckill":res.reverse().slice(6,res.length)
    }
    
    send(ctx,200,'200','查询折扣秒杀列表成功',obj)
  }

  async getfilterShopCondition(ctx){
    let res = [
      {
        name:'综合排序',
        condition:FILTER_COMPOSITE
      },
      {
        name:'优惠力度最大',
        condition:FILTER_DISCOUNT
      },
      {
        name:'起送价最低',
        condition:FILTER_MIN_PRICE
      },
      {
        name:'配送费最低',
        condition:FILTER_DISTRIBUTION
      }
    ]
    send(ctx,200,'200','获取商店筛选条件列表成功',res)
  }

  async filterShop(ctx){
    let {condition = FILTER_COMPOSITE,pageIndex = 1,pageSize = 10} = ctx.request.body
    if(!condition) condition = FILTER_COMPOSITE; //防止传空字符或者不传
    let list = await service.filterShop(FILTER_ORDER_BY_OBJECT[condition],getOffsetFn(pageIndex,pageSize),pageSize)
    let totalSize = await service.getAllFoodShopTotalSize()
    let isNextPage = isNextPageFn(pageIndex,pageSize,totalSize.totalSize)
    send(ctx,200,'200','查询列表成功',list,{...totalSize,isNextPage})    
  }

  async getShopCommentById(ctx){
    let {shopId,pageIndex = 1,pageSize = 10} = ctx.request.body
    let list = await service.getShopCommentById(shopId,getOffsetFn(pageIndex,pageSize),pageSize)
    let totalSize = await service.getShopCommentTotalSize(shopId)
    let isNextPage = isNextPageFn(pageIndex,pageSize,totalSize.totalSize)
    send(ctx,200,'200','查询列表成功',list,{...totalSize,isNextPage})
  }

  async getShopByClassifId(ctx){
    let {classifyId} = ctx.request.body;
    let res = await service.getShopByClassifId(classifyId)
    send(ctx,200,'200','查询折扣秒杀列表成功',res)
  }
}

module.exports = new FoodController()